package io.renren.modules.weixin;

import io.renren.modules.weixin.Service.AccessTokenService;
import io.renren.modules.weixin.Service.WeixinService;
import io.renren.modules.weixin.Util.WeixinUtil;
import io.renren.modules.weixin.entity.*;
import io.renren.modules.weixin.message.Industry;
import io.renren.modules.weixin.message.menu.Button;
import io.renren.modules.weixin.message.menu.CommonButton;
import io.renren.modules.weixin.message.menu.ComplexButton;
import io.renren.modules.weixin.message.menu.Menu;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;


import java.util.Date;

/**
 * @author: Mr-wang
 * @date: 2018/4/18 0018 14:23
 * @Description:
 */
@Configuration
public class WXStartupRunner implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private AccessTokenService accessTokenService;
    @Autowired
    private WeixinService weixinService;

    public static String wx_menu = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";
    public static String wx_industry = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token=ACCESS_TOKEN";
    public static String get_all_industry = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=ACCESS_TOKEN";
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {

        String token = weixinService.getToken();
/*        String token = weixinService.getToken();
        int menu_result = WeixinUtil.createTemplate(wx_menu, "菜单", getMenu(), token);
        // 判断菜单创建结果
        if (0 == menu_result)
            System.out.println("菜单创建成功！");
        else
            System.out.println("菜单创建失败，错误码：" + menu_result);

        int industry_result = WeixinUtil.createTemplate(wx_industry, "模版", getIndustry(), token);
        // 判断模版创建结果
        if (0 == industry_result)
            System.out.println("模版创建成功！");
        else
            System.out.println("模版创建失败，错误码：" + industry_result);*/

/*        JSONObject jsonObject = WeixinUtil.createTemplate(get_all_industry,token);
        System.out.println("***********"+jsonObject);*/
    }

    /**
     * 组装菜单数据
     *
     * @return
     */
    private static Menu getMenu() {
        CommonButton btn11 = new CommonButton();
        btn11.setName("绑定数据源");
        btn11.setType("click");
        btn11.setKey("11");

        CommonButton btn12 = new CommonButton();
        btn12.setName("打开自动预警");
        btn12.setType("click");
        btn12.setKey("12");

        CommonButton btn13 = new CommonButton();
        btn13.setName("关闭自动预警");
        btn13.setType("click");
        btn13.setKey("13");

        CommonButton btn21 = new CommonButton();
        btn21.setName("一键快查");
        btn21.setType("click");
        btn21.setKey("21");

        CommonButton btn31 = new CommonButton();
        btn31.setName("更多信息");
        btn31.setType("click");
        btn31.setKey("31");

        ComplexButton mainBtn1 = new ComplexButton();
        mainBtn1.setName("数据管理");
        mainBtn1.setSub_button(new CommonButton[] { btn11, btn12, btn13});

        Menu menu = new Menu();
        menu.setButton(new Button[] { mainBtn1, btn21, btn31 });
        return menu;
    }

    /**
     * 组装模版数据
     */
     private static Industry getIndustry(){
         Industry in = new Industry();
         in.setIndustry_id1("1");
         in.setIndustry_id2("2");
         return in;
     }

}